<?php
/**
 * Created by PhpStorm.
 * Email: jhasheng@hotmail.com
 * Date: 2015/5/24
 * Time: 23:13
 */

namespace Home\Controller;

use Think\Controller;

class PermissionController extends Controller
{
    public function editrole()
    {
        $authGroupModel = D('AuthGroup');
        if(IS_POST){
            if($authGroupModel->create()){
                if($authGroupModel->save()){
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error($authGroupModel->getError());
            }
        }else{
            $id = I('GET.id', 0, 'intval');
            if(!$id){
                $this->error('不存在的角色');
            }
            $authRule = $authGroupModel->getById($id);
            $ruleArr = explode(',', $authRule['rules']);
            $rules = D('AuthRule')->field(array('id', 'title'))->select();

            $this->assign('hasrules', $ruleArr);
            $this->assign('allrules', $rules);
            $this->assign('info', $authRule);
            $this->assign('action', U('Permission/editrole'));
            $this->display('addrole');
        }
    }

    public function addrole()
    {
        $this->display();
    }

    public function editnode()
    {
        $authRuleModel = D('AuthRule');
        if(IS_POST){
            if($authRuleModel->create()){
                if($authRuleModel->save()){
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error($authRuleModel->getError());
            }
        }else{
            $id = I('GET.id', 0, 'intval');
            if(!$id){
                $this->error('不存在的节点');
            }
            $authRule = $authRuleModel->getById($id);
            $this->assign('info', $authRule);
            $this->assign('action', U('Permission/editnode'));
            $this->display('addnode');
        }

    }

    public function addnode()
    {
        $authRuleModel = D('AuthRule');
        if(IS_POST){
            if($authRuleModel->create()){
                if($authRuleModel->add()){
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($authRuleModel->getError());
            }
        }else{
            $this->assign('action', U('Permission/addnode'));
            $this->display();
        }
    }

    public function listrole()
    {
        $authGroupModel = D('AuthGroup');
        $roles = $authGroupModel->select();
        $this->assign('roles', $roles);
        $this->display();
    }

    public function listnode()
    {
        $nodes = D('AuthRule')->select();
        $this->assign('nodes', $nodes);
        $this->display();
    }


}